Database Schema Design Normalization - Set 1

1. What is Database Normalization?

Database normalization is the process of structuring a relational database to minimize redundancy and dependency by organizing fields and table relations.

2. What are the different Normal Forms?

The normal forms are:

3. Explain First Normal Form (1NF) with an example.

1NF ensures that all attributes contain only atomic values and each column contains values of a single type.

            CREATE TABLE Student (
                StudentID INT PRIMARY KEY,
                Name VARCHAR(100),
                Subjects VARCHAR(255) -- Not in 1NF if multiple subjects stored in one column
            );
        
4. How does Second Normal Form (2NF) improve database structure?

2NF removes partial dependencies, ensuring that non-key attributes are fully functionally dependent on the primary key.

5. Provide an example of a table violating 2NF and how to correct it.

Example of a table violating 2NF:

            CREATE TABLE Orders (
                OrderID INT,
                ProductID INT,
                ProductName VARCHAR(100),
                PRIMARY KEY (OrderID, ProductID)
            );
        

To correct it, split into two tables:

            CREATE TABLE Orders (
                OrderID INT PRIMARY KEY
            );
            CREATE TABLE OrderDetails (
                OrderID INT,
                ProductID INT,
                ProductName VARCHAR(100),
                PRIMARY KEY (OrderID, ProductID),
                FOREIGN KEY (OrderID) REFERENCES Orders(OrderID)
            );
        
6. Explain Third Normal Form (3NF) with an example.

3NF eliminates transitive dependencies, meaning that non-key attributes should not depend on other non-key attributes.

7. What is Boyce-Codd Normal Form (BCNF)?

BCNF is a stricter version of 3NF where every determinant is a candidate key.

8. How does normalization affect database performance?

Normalization reduces redundancy, improving data integrity, but may require more joins, potentially impacting query performance.

9. When should you denormalize a database?

Denormalization is useful when read performance is critical, reducing joins at the cost of redundancy.

10. Provide a Java example for executing a normalized database query.
            import java.sql.Connection;
            import java.sql.DriverManager;
            import java.sql.PreparedStatement;
            import java.sql.ResultSet;

            public class DatabaseQueryExample {
                public static void main(String[] args) {
                    try {
                        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "password");
                        PreparedStatement stmt = conn.prepareStatement("SELECT * FROM Students WHERE StudentID = ?");
                        stmt.setInt(1, 1);
                        ResultSet rs = stmt.executeQuery();
                        while (rs.next()) {
                            System.out.println(rs.getString("Name"));
                        }
                        conn.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        

 

Database Schema Design Normalization - Set 2

11. What is the primary purpose of normalization?

Normalization primarily aims to reduce redundancy and improve data integrity.

12. Can a table be in 2NF but not in 1NF?

No, a table must be in 1NF before it can be considered for 2NF.

13. How do you handle many-to-many relationships in database design?

Many-to-many relationships are handled using a junction table.

            CREATE TABLE StudentCourse (
                StudentID INT,
                CourseID INT,
                PRIMARY KEY (StudentID, CourseID),
                FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
                FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
            );
        
14. What is the difference between 3NF and BCNF?

BCNF is stricter than 3NF as it requires that every determinant is a candidate key.

15. Why is BCNF not always desirable?

BCNF can lead to complex schemas with more tables, increasing the number of joins required for queries.

16. What is an example of a table violating BCNF?

A table where a non-primary attribute functionally determines another key violates BCNF.

17. What is Fourth Normal Form (4NF)?

4NF eliminates multi-valued dependencies to prevent redundancy.

18. How does 4NF improve database design?

It ensures that each multi-valued attribute is stored in a separate table.

19. What is a multi-valued dependency?

A multi-valued dependency occurs when one attribute depends on another, independent of other attributes.

20. Provide a Java example for handling database normalization in an ORM framework.
            @Entity
            @Table(name = "students")
            public class Student {
                @Id
                @GeneratedValue(strategy = GenerationType.IDENTITY)
                private Long id;
                private String name;
                
                @OneToMany(mappedBy = "student")
                private List enrollments;
            }
        

 

Database Schema Design Normalization - Set 3

21. What is Fifth Normal Form (5NF)?

5NF deals with join dependencies and ensures that the decomposition of relations does not result in data loss.

22. What is the importance of 5NF in database normalization?

5NF helps in removing redundancy caused by join dependencies that are not implied by candidate keys.

23. What is Domain-Key Normal Form (DKNF)?

DKNF ensures that all constraints and dependencies are enforced by domain and key constraints.

24. How does denormalization differ from normalization?

Denormalization is the process of merging tables to optimize query performance at the cost of redundancy.

25. When should denormalization be used?

Denormalization should be used when database performance is more critical than eliminating redundancy.

26. Can you provide an example of a denormalized table?
            CREATE TABLE Orders (
                OrderID INT PRIMARY KEY,
                CustomerName VARCHAR(255),
                ProductName VARCHAR(255),
                Quantity INT
            );
        
27. How do indexes help in query performance for normalized tables?

Indexes speed up searches and retrieval by providing quick lookup mechanisms.

28. How can you create an index in SQL?
            CREATE INDEX idx_customer_name ON Customers(CustomerName);
        
29. What is a foreign key constraint?

A foreign key constraint ensures referential integrity between two tables.

30. Provide a Java example of defining a foreign key using JPA.
            @Entity
            @Table(name = "orders")
            public class Order {
                @Id
                @GeneratedValue(strategy = GenerationType.IDENTITY)
                private Long id;
                
                @ManyToOne
                @JoinColumn(name = "customer_id", nullable = false)
                private Customer customer;
            }
        

 

Database Schema Design Normalization - Set 4

31. What is the difference between star and snowflake schema?

Star schema has denormalized dimensions, while snowflake schema normalizes dimensions to reduce redundancy.

32. What are the advantages of a star schema?

Star schema offers simpler queries and faster retrieval due to fewer joins.

33. What is a surrogate key in database design?

A surrogate key is a unique, system-generated key used as a primary key instead of a natural key.

34. How does normalization affect indexing?

Normalization increases the number of tables, requiring careful indexing strategies to maintain performance.

35. What is a composite key?

A composite key is a primary key that consists of two or more columns.

36. Provide an SQL example of a composite key.
            CREATE TABLE Enrollment (
                StudentID INT,
                CourseID INT,
                PRIMARY KEY (StudentID, CourseID)
            );
        
37. What is the purpose of a unique constraint?

A unique constraint ensures that all values in a column or set of columns are distinct.

38. How do you define a unique constraint in SQL?
            CREATE TABLE Employees (
                EmployeeID INT PRIMARY KEY,
                Email VARCHAR(255) UNIQUE
            );
        
39. What is the role of a normalization tool?

Normalization tools help automate database schema optimization to achieve normalization levels.

40. Provide a Java example using JPA with a composite key.
            @Embeddable
            public class EnrollmentId implements Serializable {
                private Long studentId;
                private Long courseId;
            }

            @Entity
            @Table(name = "enrollment")
            public class Enrollment {
                @EmbeddedId
                private EnrollmentId id;
            }
        

 

 

Database Schema Design Normalization - Set 5

41. What is a natural key in database design?

A natural key is a key that has a real-world meaning, such as a Social Security Number or email address.

42. What is a candidate key?

A candidate key is a column, or set of columns, that can uniquely identify a record in a table.

43. How is a foreign key different from a primary key?

A primary key uniquely identifies records in its own table, while a foreign key references a primary key in another table.

44. Provide an SQL example of defining a foreign key.
            CREATE TABLE Orders (
                OrderID INT PRIMARY KEY,
                CustomerID INT,
                FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
            );
        
45. What is a transitive dependency?

A transitive dependency occurs when a non-key attribute depends on another non-key attribute, violating 3NF.

46. How does Boyce-Codd Normal Form (BCNF) differ from 3NF?

BCNF is a stricter form of 3NF that removes any remaining functional dependencies where non-key attributes determine part of the key.

47. What is the role of denormalization in database design?

Denormalization reduces joins and improves performance by reintroducing redundancy into a normalized schema.

48. Provide an SQL example of denormalization.
            CREATE TABLE Orders (
                OrderID INT PRIMARY KEY,
                CustomerName VARCHAR(255),
                ProductName VARCHAR(255)
            );
        
49. What is 4th Normal Form (4NF)?

4NF eliminates multi-valued dependencies, ensuring that each record represents a single fact.

50. Provide a Java JPA example for handling a foreign key relationship.
            @Entity
            public class Order {
                @Id
                @GeneratedValue(strategy = GenerationType.IDENTITY)
                private Long orderId;
                
                @ManyToOne
                @JoinColumn(name = "customer_id")
                private Customer customer;
            }
        

 

Database Schema Design Normalization - Set 6

51. What is the difference between normalization and denormalization?

Normalization reduces redundancy and ensures data integrity, while denormalization improves read performance by reducing joins.

52. How does 5th Normal Form (5NF) differ from 4NF?

5NF ensures that all join dependencies are lossless and eliminates redundancy due to multi-join relationships.

53. What is a surrogate key?

A surrogate key is an artificially generated unique identifier, such as an auto-incremented ID.

54. Provide an example of using a surrogate key in SQL.
            CREATE TABLE Customers (
                CustomerID SERIAL PRIMARY KEY,
                Name VARCHAR(255) NOT NULL
            );
        
55. What is domain-key normal form (DKNF)?

DKNF ensures that every constraint on the database is a logical consequence of domain and key constraints.

56. How do indexing and normalization relate?

Indexes improve query performance, while normalization optimizes storage and data integrity.

57. Provide a Java JPA example for using a composite key.
            @Embeddable
            public class OrderKey implements Serializable {
                private Long orderId;
                private Long productId;
            }
            
            @Entity
            public class Order {
                @EmbeddedId
                private OrderKey id;
            }
        
58. What is a functional dependency?

A functional dependency occurs when one attribute uniquely determines another attribute in a relation.

59. Provide an SQL example of creating an index.
            CREATE INDEX idx_customer_name ON Customers(Name);
        
60. What is the difference between a clustered and a non-clustered index?

A clustered index determines the physical order of data in a table, while a non-clustered index maintains a separate structure for lookups.

 

Database Schema Design Normalization - Set 7

61. What is a transitive dependency?

A transitive dependency exists when a non-key attribute depends on another non-key attribute instead of the primary key.

62. Provide an SQL example to remove transitive dependencies.
            CREATE TABLE Employees (
                EmployeeID INT PRIMARY KEY,
                DepartmentID INT,
                DepartmentName VARCHAR(255)
            );
            
            -- Normalize by creating a separate Department table
            CREATE TABLE Departments (
                DepartmentID INT PRIMARY KEY,
                DepartmentName VARCHAR(255)
            );
        
63. What is the difference between 2NF and 3NF?

2NF removes partial dependencies, while 3NF removes transitive dependencies.

64. How does BCNF improve on 3NF?

BCNF ensures that every determinant is a candidate key, further reducing redundancy.

65. Provide a Java JPA example for a normalized entity relationship.
            @Entity
            public class Employee {
                @Id
                @GeneratedValue
                private Long id;
                
                @ManyToOne
                @JoinColumn(name = "department_id")
                private Department department;
            }
            
            @Entity
            public class Department {
                @Id
                @GeneratedValue
                private Long id;
                private String name;
            }
        
66. What is a superkey?

A superkey is a set of attributes that uniquely identifies a tuple in a relation.

67. How does a superkey differ from a candidate key?

A candidate key is a minimal superkey that contains no unnecessary attributes.

68. Provide an SQL example for defining a composite primary key.
            CREATE TABLE OrderDetails (
                OrderID INT,
                ProductID INT,
                Quantity INT,
                PRIMARY KEY (OrderID, ProductID)
            );
        
69. What is the purpose of 6th Normal Form (6NF)?

6NF deals with temporal databases by eliminating non-trivial join dependencies.

70. How can normalization impact performance?

Normalization improves data integrity but may require additional joins, potentially impacting query performance.

 

Database Schema Design Normalization - Set 8

71. What is the main goal of 4th Normal Form (4NF)?

4NF eliminates multi-valued dependencies to prevent redundancy in multi-valued fields.

72. Provide an example of a multi-valued dependency.
            CREATE TABLE EmployeeSkills (
                EmployeeID INT,
                Skill VARCHAR(255),
                Certification VARCHAR(255)
            );
        
73. How do you normalize a table to 4NF?

By splitting multi-valued attributes into separate tables.

            CREATE TABLE EmployeeSkills (
                EmployeeID INT,
                Skill VARCHAR(255),
                PRIMARY KEY (EmployeeID, Skill)
            );
            
            CREATE TABLE EmployeeCertifications (
                EmployeeID INT,
                Certification VARCHAR(255),
                PRIMARY KEY (EmployeeID, Certification)
            );
        
74. What is 5th Normal Form (5NF)?

5NF eliminates join dependencies by ensuring all data is represented without redundant decomposition.

75. Provide a Java Hibernate example to implement 4NF.
            @Entity
            public class EmployeeSkill {
                @Id
                @GeneratedValue
                private Long id;
                
                private String skill;
                
                @ManyToOne
                @JoinColumn(name = "employee_id")
                private Employee employee;
            }
        
76. What is a multi-valued dependency?

It occurs when one attribute determines multiple independent attributes.

77. How does 5NF improve database integrity?

It removes redundancy from complex join dependencies.

78. Provide an SQL example of a table violating 5NF.
            CREATE TABLE Projects (
                EmployeeID INT,
                ProjectID INT,
                Role VARCHAR(255),
                PRIMARY KEY (EmployeeID, ProjectID, Role)
            );
        
79. How do you normalize a table to 5NF?

By decomposing the table into smaller tables that eliminate redundancy.

80. Why is 5NF rarely implemented in practice?

Because most real-world databases do not require this level of normalization due to performance concerns.

 

Database Schema Design Normalization - Set 9

81. What is 6th Normal Form (6NF)?

6NF deals with temporal data and ensures that no redundancy exists by breaking down data into the smallest possible parts.

82. Provide an example of a table in 6NF.
            CREATE TABLE EmployeeHistory (
                EmployeeID INT,
                StartDate DATE,
                EndDate DATE,
                Department VARCHAR(255),
                PRIMARY KEY (EmployeeID, StartDate)
            );
        
83. How does 6NF improve data consistency?

By ensuring that data changes over time are tracked precisely without redundancy.

84. What is the main difference between 5NF and 6NF?

5NF eliminates join dependencies, while 6NF focuses on temporal dependencies.

85. Provide a Java Hibernate example handling temporal data in 6NF.
            @Entity
            public class EmployeeHistory {
                @Id
                @GeneratedValue
                private Long id;
                
                private LocalDate startDate;
                private LocalDate endDate;
                private String department;
            }
        
86. What are the performance considerations when using 6NF?

6NF can lead to a high number of joins and may impact query performance negatively.

87. How can indexes improve performance in highly normalized databases?

Indexes help optimize search queries by speeding up lookups in highly decomposed tables.

88. Provide an SQL example creating an index in a normalized table.
            CREATE INDEX idx_employee_history ON EmployeeHistory (EmployeeID, StartDate);
        
89. When is normalization not recommended?

Normalization is not recommended when performance is a priority over data integrity, such as in read-heavy applications.

90. What is denormalization, and why is it used?

Denormalization is the process of combining tables to reduce joins and improve read performance at the cost of redundancy.